<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="formData.nickname" disabled />
      </el-form-item>
      <el-form-item label="黄金价格" prop="auPrice">
        <el-input v-model="formData.auPrice" placeholder="请输入黄金价格" />
      </el-form-item>
      <el-form-item label="铂金价格" prop="ptPrice">
        <el-input v-model="formData.ptPrice" placeholder="请输入铂金价格" />
      </el-form-item>
      <el-form-item label="钯金价格" prop="pdPrice">
        <el-input v-model="formData.pdPrice" placeholder="请输入钯金价格" />
      </el-form-item>
      <el-form-item label="白银价格" prop="agPrice">
        <el-input v-model="formData.agPrice" placeholder="请输入白银价格" />
      </el-form-item>
      <el-form-item label="黄金销售价格" prop="auSalePrice">
        <el-input v-model="formData.auSalePrice" placeholder="请输入黄金销售价格" />
      </el-form-item>
      <el-form-item label="铂金销售价格" prop="ptSalePrice">
        <el-input v-model="formData.ptSalePrice" placeholder="请输入铂金销售价格" />
      </el-form-item>
      <el-form-item label="钯金销售价格" prop="pdSalePrice">
        <el-input v-model="formData.pdSalePrice" placeholder="请输入钯金销售价格" />
      </el-form-item>
      <el-form-item label="白银销售价格" prop="agSalePrice">
        <el-input v-model="formData.agSalePrice" placeholder="请输入白银销售价格" />
      </el-form-item>
      <el-form-item label="金条9999价格" prop="au9999Price">
        <el-input v-model="formData.au9999Price" placeholder="请输入金条9999价格" />
      </el-form-item>
      <el-form-item label="千足金价格" prop="au999Price">
        <el-input v-model="formData.au999Price" placeholder="请输入千足金价格" />
      </el-form-item>
      <el-form-item label="900价格" prop="au900Price">
        <el-input v-model="formData.au900Price" placeholder="请输入900价格" />
      </el-form-item>
      <el-form-item label="22k/916价格" prop="au916Price">
        <el-input v-model="formData.au916Price" placeholder="请输入22k/916价格" />
      </el-form-item>
      <el-form-item label="18k/750价格" prop="au750Price">
        <el-input v-model="formData.au750Price" placeholder="请输入18k/750价格" />
      </el-form-item>
      <el-form-item label="14k/585价格" prop="au585Price">
        <el-input v-model="formData.au585Price" placeholder="请输入14k/585价格" />
      </el-form-item>
      <el-form-item label="Pt990价格" prop="pt990Price">
        <el-input v-model="formData.pt990Price" placeholder="请输入Pt990价格" />
      </el-form-item>
      <el-form-item label="Pt950价格" prop="pt950Price">
        <el-input v-model="formData.pt950Price" placeholder="请输入Pt950价格" />
      </el-form-item>
      <el-form-item label="Pt900价格" prop="pt900Price">
        <el-input v-model="formData.pt900Price" placeholder="请输入Pt900价格" />
      </el-form-item>
      <el-form-item label="Pd990价格" prop="pd990Price">
        <el-input v-model="formData.pd990Price" placeholder="请输入Pd990价格" />
      </el-form-item>
      <el-form-item label="Pd950价格" prop="pd950Price">
        <el-input v-model="formData.pd950Price" placeholder="请输入Pd950价格" />
      </el-form-item>
      <el-form-item label="Ag999价格" prop="ag999Price">
        <el-input v-model="formData.ag999Price" placeholder="请输入Ag999价格" />
      </el-form-item>
      <el-form-item label="Ag925价格" prop="ag925Price">
        <el-input v-model="formData.ag925Price" placeholder="请输入Ag925价格" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as MetalPriceConfigApi from '@/api/along/metalpriceconfig'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  nickname: undefined,
  configType: undefined,
  auPrice: undefined,
  ptPrice: undefined,
  pdPrice: undefined,
  agPrice: undefined,
  auSalePrice: undefined,
  ptSalePrice: undefined,
  pdSalePrice: undefined,
  agSalePrice: undefined,
  au9999Price: undefined,
  au999Price: undefined,
  au900Price: undefined,
  au916Price: undefined,
  au750Price: undefined,
  au585Price: undefined,
  pt990Price: undefined,
  pt950Price: undefined,
  pt900Price: undefined,
  pd990Price: undefined,
  pd950Price: undefined,
  ag999Price: undefined,
  ag925Price: undefined,
})
const formRules = reactive({
  auPrice: [{ required: true, message: '黄金价格不能为空', trigger: 'blur' }],
  ptPrice: [{ required: true, message: '铂金价格不能为空', trigger: 'blur' }],
  pdPrice: [{ required: true, message: '钯金价格不能为空', trigger: 'blur' }],
  agPrice: [{ required: true, message: '白银价格不能为空', trigger: 'blur' }],
  auSalePrice: [{ required: true, message: '黄金销售价格不能为空', trigger: 'blur' }],
  ptSalePrice: [{ required: true, message: '铂金销售价格不能为空', trigger: 'blur' }],
  pdSalePrice: [{ required: true, message: '钯金销售价格不能为空', trigger: 'blur' }],
  agSalePrice: [{ required: true, message: '白银销售价格不能为空', trigger: 'blur' }],
  au9999Price: [{ required: true, message: '金条9999价格不能为空', trigger: 'blur' }],
  au999Price: [{ required: true, message: '千足金价格不能为空', trigger: 'blur' }],
  au900Price: [{ required: true, message: '900价格不能为空', trigger: 'blur' }],
  au916Price: [{ required: true, message: '22k/916价格不能为空', trigger: 'blur' }],
  au750Price: [{ required: true, message: '18k/750价格不能为空', trigger: 'blur' }],
  au585Price: [{ required: true, message: '14k/585价格不能为空', trigger: 'blur' }],
  pt990Price: [{ required: true, message: 'Pt990价格不能为空', trigger: 'blur' }],
  pt950Price: [{ required: true, message: 'Pt950价格不能为空', trigger: 'blur' }],
  pt900Price: [{ required: true, message: 'Pt900价格不能为空', trigger: 'blur' }],
  pd990Price: [{ required: true, message: 'Pd990价格不能为空', trigger: 'blur' }],
  pd950Price: [{ required: true, message: 'Pd950价格不能为空', trigger: 'blur' }],
  ag999Price: [{ required: true, message: 'Ag999价格不能为空', trigger: 'blur' }],
  ag925Price: [{ required: true, message: 'Ag925价格不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MetalPriceConfigApi.getMetalPriceConfig(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MetalPriceConfigApi.MetalPriceConfigVO
    if (formType.value === 'create') {
      await MetalPriceConfigApi.createMetalPriceConfig(data)
      message.success(t('common.createSuccess'))
    } else {
      await MetalPriceConfigApi.updateMetalPriceConfig(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    configType: undefined,
    auPrice: undefined,
    ptPrice: undefined,
    pdPrice: undefined,
    agPrice: undefined,
    auSalePrice: undefined,
    ptSalePrice: undefined,
    pdSalePrice: undefined,
    agSalePrice: undefined,
    au9999Price: undefined,
    au999Price: undefined,
    au900Price: undefined,
    au916Price: undefined,
    au750Price: undefined,
    au585Price: undefined,
    pt990Price: undefined,
    pt950Price: undefined,
    pt900Price: undefined,
    pd990Price: undefined,
    pd950Price: undefined,
    ag999Price: undefined,
    ag925Price: undefined,
  }
  formRef.value?.resetFields()
}
</script>
